#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
@author:  runyuanye
@contact: runyuanye@163.com
"""

import argparse
import os
import sys

sys.path.append('.')

"""
    将视频转换为图片； 
"""


def main():
    parser = argparse.ArgumentParser(description="Convert Video To Image")
    parser.add_argument(
        "--video_dir", default='/rootfs/media/kasim/DataSet/FaceVideo', help="path to video file", type=str
    )
    parser.add_argument(
        "--video_list", default='/rootfs/media/kasim/DataSet/FaceVideo/file_list.txt', help="video file list", type=str
    )
    parser.add_argument(
        "--out_dir", default='/rootfs/media/kasim/DataSet/FaceVideo/images', help="path to output", type=str
    )
    args = parser.parse_args()

    video_list = []
    with open(args.video_list, 'r') as file:
        for line in file.readlines():
            file_name = line.split(',')[0].strip()
            video_list.append(file_name)

    out_dir = args.out_dir
    if not os.path.exists(out_dir):
        os.makedirs(out_dir)
    os.system('chmod a+wr {}'.format(out_dir))

    file_count = len(video_list)
    for idx, video_name in enumerate(video_list):
        video_path = os.path.join(args.video_dir, video_name)
        print('{}/{} {}'.format(idx+1, file_count, video_path))
        out_dir_name = os.path.splitext(video_name)[0]
        out_dir_path = os.path.join(out_dir, out_dir_name)

        if not os.path.exists(out_dir_path):
            os.makedirs(out_dir_path)

        conv_cmd_str = 'ffmpeg -i {} -c:v copy {}/F%04dB00.jpg'.format(video_path, out_dir_path)
        os.system(conv_cmd_str)

        os.system('chmod a+wr {} -Rf'.format(out_dir_path))

    print('finish!')


if __name__ == '__main__':
    main()
